﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using cpe.cpein.DATA;

namespace cpe.cpein.BUSINESS
{
    public static class BUNotification
    {
        /// <summary>
        /// Get All the Notification of the database.
        /// </summary>
        /// <param name="searchInDeleted"></param>
        /// <returns></returns>
        public static List<Notification> GetAllNotification(bool searchInDeleted)
        {
            List<Notification> Notifications = new List<Notification>();
            using (cpeinEntities db = new cpeinEntities())
            {
                var results = from Notification in db.Notification
                              where Notification.ntf_isdeleted == searchInDeleted
                              select Notification;
                foreach (Notification tmp in results)
                    Notifications.Add(tmp);
            }

            return Notifications;
        }

        /// <summary>
        /// Get the Notification by id.
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static Notification GetNotificationByID(int id)
        {
            if (id <= 0)
                return null;
            else
            {
                using (cpeinEntities db = new cpeinEntities())
                {
                    var ntf = from Notification in db.Notification
                              where Notification.ntf_id == id
                              select Notification;
                    foreach (Notification tmp in ntf)
                        return tmp;
                    return null;
                }
            }
        }

        /// <summary>
        /// ntf a Notification into the database.
        /// </summary>
        /// <param name="ntf"></param>
        /// <returns></returns>
        public static Notification Notificationrea(Notification ntf)
        {
            if (ntf != null)
            {
                using (cpeinEntities db = new cpeinEntities())
                {
                    Notification nntf = db.Notification.Add(ntf);
                    db.SaveChanges();
                    return nntf;
                }
            }
            else
                return null;
        }

        /// <summary>
        /// Remove a specific Notification of the database.
        /// </summary>
        /// <param name="ntf"></param>
        /// <returns></returns>
        public static bool RemoveNotification(Notification ntf)
        {
            if (ntf != null)
            {
                ntf.ntf_isdeleted = true;
                if (UpdateNotification(ntf) != null)
                {
                    using (cpeinEntities db = new cpeinEntities())
                    {
                        db.SaveChanges();
                        return true;
                    }
                }
            }

            return false;
        }

        /// <summary>
        /// Update the Notification into the database.
        /// </summary>
        /// <param name="ntf"></param>
        /// <returns></returns>
        public static Notification UpdateNotification(Notification ntf)
        {
            if (ntf != null)
            {
                using (cpeinEntities db = new cpeinEntities())
                {
                    var current = from Notification in db.Notification
                                  where Notification.ntf_id == ntf.ntf_id
                                  select Notification;

                    foreach (var tmp in current)
                    {
                        tmp.usr_id = ntf.usr_id;
                        tmp.ntf_content = ntf.ntf_content;
                        tmp.ntf_ispublic = ntf.ntf_ispublic;
                        tmp.ntf_title = ntf.ntf_title;
                        tmp.ntf_isdeleted = ntf.ntf_isdeleted;

                        db.SaveChanges();
                    }

                    ntf = current as Notification;
                }
            }

            return ntf;
        }
    }
}
